import { ConfigProvider as AntConfigProvider } from 'antd';
import React from 'react';
import defaultThemeConfig from './../../theme/themeConfig';
import type { ConfigContextType } from './context';
import { ConfigProvider, useConfig } from './context';
import type { ConfigProviderProps } from './interface';
import './styles/index.less';

/** 全局配置组件 */
const ConfigProviderComponent: React.FC<ConfigProviderProps> = (props) => {
  const { antNative = false, className, children, theme, ...restProps } = props;

  // 如果使用原生属性，直接返回 antd 组件
  if (antNative) {
    return <AntConfigProvider {...restProps}>{children}</AntConfigProvider>;
  }

  // 处理主题配置
  const themeConfig = theme
    ? {
        ...defaultThemeConfig,
        token: {
          colorPrimary: theme.primaryColor,
          ...(theme.isDark
            ? {
                colorBgContainer: '#141414',
                colorText: 'rgba(255, 255, 255, 0.85)',
              }
            : {}),
        },
      }
    : defaultThemeConfig;

  return (
    <AntConfigProvider theme={themeConfig} {...restProps}>
      {children}
    </AntConfigProvider>
  );
};

export type { ConfigProviderProps };
export { ConfigProvider, useConfig };
export type { ConfigContextType };
export default ConfigProviderComponent;
